// 角色属性系统
class CharacterSystem {
    constructor() {
        this.attributes = {
            scientific: 50,
            courage: 50,
            caution: 50,
            empathy: 50
        };
        
        this.characterTypes = {
            scientific_researcher: { scientific: 70, courage: 40, caution: 60, empathy: 50 },
            brave_explorer: { scientific: 40, courage: 80, caution: 30, empathy: 60 },
            cautious_analyst: { scientific: 60, courage: 30, caution: 80, empathy: 40 },
            empathetic_guide: { scientific: 50, courage: 50, caution: 50, empathy: 80 }
        };
        
        this.choiceEffects = {
            // 选择对属性的影响
            scientific_choice: { scientific: 10, courage: 0, caution: 5, empathy: 0 },
            brave_choice: { scientific: 0, courage: 15, caution: -5, empathy: 5 },
            cautious_choice: { scientific: 5, courage: -5, caution: 15, empathy: 0 },
            empathetic_choice: { scientific: 0, courage: 5, caution: 0, empathy: 10 }
        };
        
        this.init();
    }
    
    init() {
        this.setupCharacterPanel();
        this.setupChoicePreview();
        console.log('✓ 角色系统已初始化');
    }
    
    setupCharacterPanel() {
        const toggleBtn = document.getElementById('toggleCharacterPanelBtn');
        const characterPanel = document.getElementById('characterPanel');
        
        if (toggleBtn && characterPanel) {
            toggleBtn.addEventListener('click', () => {
                this.toggleCharacterPanel();
            });
        }
        
        this.updateCharacterDisplay();
    }
    
    toggleCharacterPanel() {
        const characterPanel = document.getElementById('characterPanel');
        const gameContainer = document.querySelector('.game-container');
        
        if (characterPanel && gameContainer) {
            const isHidden = characterPanel.classList.contains('hidden');
            
            if (isHidden) {
                characterPanel.classList.remove('hidden');
                gameContainer.classList.remove('character-panel-hidden');
            } else {
                characterPanel.classList.add('hidden');
                gameContainer.classList.add('character-panel-hidden');
            }
        }
    }
    
    setupChoicePreview() {
        // 监听选择按钮的悬停事件
        document.addEventListener('mouseover', (e) => {
            if (e.target.classList.contains('choice-btn')) {
                this.showChoicePreview(e.target);
            }
        });
        
        document.addEventListener('mouseout', (e) => {
            if (e.target.classList.contains('choice-btn')) {
                this.hideChoicePreview();
            }
        });
    }
    
    showChoicePreview(choiceElement) {
        const choiceData = this.getChoiceData(choiceElement);
        if (!choiceData) return;
        
        const previewContainer = document.getElementById('choicePreview');
        const previewText = document.getElementById('choicePreviewText');
        const attributeChanges = document.getElementById('attributeChanges');
        
        if (previewContainer && previewText && attributeChanges) {
            previewText.textContent = choiceData.preview || '这个选择可能会带来意想不到的后果...';
            
            // 显示属性变化
            attributeChanges.innerHTML = this.generateAttributeChangesHTML(choiceData.effects);
            
            previewContainer.style.display = 'block';
        }
    }
    
    hideChoicePreview() {
        const previewContainer = document.getElementById('choicePreview');
        if (previewContainer) {
            previewContainer.style.display = 'none';
        }
    }
    
    getChoiceData(choiceElement) {
        const choiceId = choiceElement.dataset.choiceId || choiceElement.id;
        
        // 根据选择ID返回相应的数据
        const choiceData = {
            'choice1_1': {
                preview: '理性分析数据，科学属性可能提升',
                effects: { scientific: 10, caution: 5 }
            },
            'choice1_2': {
                preview: '勇敢探索未知，勇气属性可能提升',
                effects: { courage: 10, empathy: 5 }
            },
            'choice1_3': {
                preview: '谨慎保证安全，谨慎属性可能提升',
                effects: { caution: 15, scientific: -5 }
            }
        };
        
        return choiceData[choiceId] || null;
    }
    
    generateAttributeChangesHTML(effects) {
        if (!effects) return '';
        
        let html = '';
        Object.entries(effects).forEach(([attr, value]) => {
            if (value !== 0) {
                const className = value > 0 ? 'positive' : 'negative';
                const sign = value > 0 ? '+' : '';
                const attrName = this.getAttributeName(attr);
                
                html += `<div class="attribute-change ${className}">
                    <span>${attrName}</span>
                    <span>${sign}${value}</span>
                </div>`;
            }
        });
        
        return html;
    }
    
    getAttributeName(attr) {
        const names = {
            scientific: window.i18n?.t('scientific') || '科学',
            courage: window.i18n?.t('courage') || '勇气',
            caution: window.i18n?.t('caution') || '谨慎',
            empathy: window.i18n?.t('empathy') || '同理心'
        };
        return names[attr] || attr;
    }
    
    applyChoiceEffect(choiceId) {
        const choiceData = this.getChoiceData({ dataset: { choiceId } });
        if (choiceData && choiceData.effects) {
            Object.entries(choiceData.effects).forEach(([attr, value]) => {
                this.modifyAttribute(attr, value);
            });
            
            this.updateCharacterDisplay();
            this.updateCharacterType();
        }
    }
    
    modifyAttribute(attribute, value) {
        if (this.attributes.hasOwnProperty(attribute)) {
            this.attributes[attribute] = Math.max(0, Math.min(100, this.attributes[attribute] + value));
        }
    }
    
    updateCharacterDisplay() {
        Object.entries(this.attributes).forEach(([attr, value]) => {
            const fillElement = document.getElementById(`${attr}Attr`);
            const valueElement = document.getElementById(`${attr}Value`);
            
            if (fillElement) {
                fillElement.style.width = `${value}%`;
            }
            
            if (valueElement) {
                valueElement.textContent = value;
            }
        });
    }
    
    updateCharacterType() {
        const type = this.determineCharacterType();
        const typeElement = document.getElementById('characterTypeValue');
        
        if (typeElement && window.i18n) {
            typeElement.textContent = window.i18n.t(type);
            typeElement.setAttribute('data-i18n', type);
        }
    }
    
    determineCharacterType() {
        const { scientific, courage, caution, empathy } = this.attributes;
        
        if (scientific >= 70) return 'scientific_researcher';
        if (courage >= 70) return 'brave_explorer';
        if (caution >= 70) return 'cautious_analyst';
        if (empathy >= 70) return 'empathetic_guide';
        
        // 根据最高属性确定类型
        const maxAttr = Object.keys(this.attributes).reduce((a, b) => 
            this.attributes[a] > this.attributes[b] ? a : b
        );
        
        const typeMap = {
            scientific: 'scientific_researcher',
            courage: 'brave_explorer',
            caution: 'cautious_analyst',
            empathy: 'empathetic_guide'
        };
        
        return typeMap[maxAttr] || 'researcher';
    }
    
    getCharacterSummary() {
        return {
            attributes: { ...this.attributes },
            type: this.determineCharacterType(),
            dominantTrait: this.getDominantTrait()
        };
    }
    
    getDominantTrait() {
        const maxAttr = Object.keys(this.attributes).reduce((a, b) => 
            this.attributes[a] > this.attributes[b] ? a : b
        );
        return {
            trait: maxAttr,
            value: this.attributes[maxAttr]
        };
    }
    
    // 保存和加载
    saveCharacterData() {
        return {
            attributes: this.attributes,
            timestamp: Date.now()
        };
    }
    
    loadCharacterData(data) {
        if (data && data.attributes) {
            this.attributes = { ...this.attributes, ...data.attributes };
            this.updateCharacterDisplay();
            this.updateCharacterType();
        }
    }
    
    resetCharacter() {
        this.attributes = {
            scientific: 50,
            courage: 50,
            caution: 50,
            empathy: 50
        };
        this.updateCharacterDisplay();
        this.updateCharacterType();
    }
}

// 创建全局实例
window.characterSystem = new CharacterSystem();

// 导出到模块
if (typeof module !== 'undefined' && module.exports) {
    module.exports = CharacterSystem;
}